//
//  CZOrginal.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/4.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SnapKit

    /*
    原创微博 --
    头像
    认证图片
    微博名字
    会员等级
    发表时间
    发表来源
    发表内容
    */
let CellMargin = 10

class CZOrginal: UIView {

    //定义一个约束,用于记录底部、
    var bottomConstarint: Constraint?
    
    
    //定义一个属性
    
    var statusViewModel:CZStatusViewModel?{
    
        didSet{
        
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            
            contentLabel.text = statusViewModel?.statusModel?.text
            
            veriftyImageView.image = statusViewModel?.verifity_image
        
            levelImageView.image = statusViewModel?.level_image
            
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.statusModel?.source)
            
            timeLabel.text = statusViewModel?.dealCreateDate(statusViewModel?.statusModel?.created_at)
            
            
            //为了防止配图的约束冲突，需要清空约束
            bottomConstarint?.uninstall()
            
// MARK:           显示不显示配图视图
            if let pic_urls = statusViewModel?.statusModel?.pic_urls where
                
                pic_urls.count > 0 {
            
           // print(statusViewModel?.statusModel?.text)
                    
                    pictureView.hidden = false
                    
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        //底部视图应该等于配图视图的底部
                        bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    })
            
                    //传值
//                    pictureView.pictureViewModel = statusViewModel
                    
                    var result = [CZPictureViewModel]()
                    
                    for model in pic_urls{
                    
                    let viewModel = CZPictureViewModel()
                        
                        viewModel.model = model
                        
                        result.append(viewModel)
                    
                    
                    }
                    
                  pictureView.pictureList = result
                    
             }else{
            
            pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //没有配图视图,底部应该在 contentLabel的底部
                    bottomConstarint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            
            }
            
            
            
        }
    
    
    }
    
    /**
    *  三部曲
    */
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }

    //添加控件和约束
    private func setupUI(){
        
        addSubview(photoImageView)
        
        addSubview(veriftyImageView)
        
        addSubview(nameLabel)
        
        addSubview(levelImageView)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        
        //添加配图
        addSubview(pictureView)
        
        //添加约束头像
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
            
        }
        //认证图片添加约束 --中心
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        //微博名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
    //等级中心
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        //时间约束 -- 
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        //设置内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
            
            //make.right.equalTo(self.snp_right).offset(-CellMargin)
            
            make.width.equalTo(kUIScreenWidth - 2 * CGFloat(CellMargin))
            
            //make.bottom.equalTo(self.snp_bottom)
        }
        
        //MARK:添加配图的约束显示不显示配图视图
        //当有配图的时候,展示配图,修改约束
        //没有的时候,就隐藏,修改约束
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            //make.size.equalTo(CGSizeMake(100, 100))
        }
        
    //需要告诉视图的底部在哪里
        self.snp_makeConstraints { (make) -> Void in
            bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin).constraint
        }
        
        
        
    }
    
    //MARK:懒加载
    private lazy var photoImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
    
    
    return img
    
    }()
    
    //MARK:认证图片
    private lazy var veriftyImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    
    }()
    
   //MARK:微博的名字
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.text = "微博名字"
        
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    
    //MARK:会员等级
    
    private lazy var levelImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
    
    return img
    }()
    
    //  MARK:发表时间，发表来源
    private lazy var sourceLabel:UILabel = {
    
    let label = UILabel()
        
        label.text = "微博来源"
        
        label.font = UIFont.systemFontOfSize(13)
        
        //label.textColor = UIColor.orangeColor()
        
        return label
    }()
    
    //来源
    
    private lazy var timeLabel: UILabel = {
    
    let label = UILabel()
        
        label.text = "微博时间"
        
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.orangeColor()
    return label
    }()
    //MARK: 微博内容
    
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "微博里面的数据"
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
    
        return label
        
    }()
    
    private lazy var pictureView:CZPictureView = CZPictureView()
    
}
